package com.gimi.cloud.jimi.queue.revicer;

import com.gimi.cloud.jimi.common.mq.enume.QueueConstant;
import com.gimi.cloud.jimi.dao.dao.sys.SysMqLogDao;
import com.gimi.cloud.jimi.dto.app.moreInfo.FengchaoEmailDTO;
import com.gimi.cloud.jimi.entity.sys.SysMqLogEntity;
import com.gimi.cloud.jimi.queue.service.FengchaoCreditCardEmailService;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.Date;

/**
 * 蜂巢信用卡邮箱账单队列
 *
 * @author lilinfeng
 */
@Component
public class FengchaoCreditCardEmailReceived {

    private static final Logger log = LoggerFactory.getLogger(FengchaoCreditCardEmailReceived.class);

    @Autowired
    FengchaoCreditCardEmailService fengchaoCreditCardEmailService;
    @Autowired
    SysMqLogDao sysMqLogDao;

    /**
     * 获取报告
     *
     * @throws IOException
     */
    @RabbitListener(queues = {QueueConstant.FCXYKYXZD_QUEUE_NAME})
    public void getReport(Message message, Channel channel, FengchaoEmailDTO fengchaoEmailDTO) throws IOException {
        SysMqLogEntity mqLog = new SysMqLogEntity();
        mqLog.setName("蜂巢信用卡邮箱账单队列");
        mqLog.setStartTime(new Date());
        mqLog.setUserId(fengchaoEmailDTO.getUserId());
        mqLog.setParam(fengchaoEmailDTO.toString());
        try {
            fengchaoCreditCardEmailService.getReport(message, channel, fengchaoEmailDTO);
        } catch (Exception e) {
            log.info("获取信用卡邮箱账单失败，已自动取消!");
            e.printStackTrace();
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
        mqLog.setEndTime(new Date());
        sysMqLogDao.insertSelective(mqLog);
    }
}
